package servlet;

import util.DBUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.List;

public class EssayUpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("update");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int uid=Integer.parseInt(request.getParameter("uid"));
        int eid=Integer.parseInt(request.getParameter("eid"));
        String EssayTitle = request.getParameter("EssayTitle");
        String essay_class = request.getParameter("essay_class");
        String content = request.getParameter("editorValue");
        if(EssayTitle.equals("")||essay_class==null||content==null){
            System.out.println("内容没有填写完整");
            request.getSession().setAttribute("msg","内容没有填写完整");
            request.getSession().setAttribute("status","0");
            RequestDispatcher rd = request.getRequestDispatcher("management.jsp");
            rd.forward(request,response);
        }else {
            System.out.println("EssayTitle:"+EssayTitle);
            System.out.println("uid:"+uid);
            System.out.println("essay_class:"+essay_class);
            System.out.println("content:"+content);
            Timestamp updatetime=new Timestamp(System.currentTimeMillis());
            try { Connection conn=null;
                PreparedStatement ptmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.cj.jdbc.Driver");//注册数据库驱动
                String url = "jdbc:mysql://localhost:3306/boke?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8";
                conn= DriverManager.getConnection(url, "root", "123456");//通过DriverManager获取数据库

                    String sql1="update essay set eclass=?,update_time=?,content=?,title=?  where eid=?";
                    ptmt=conn.prepareStatement(sql1);
                    ptmt.setString(1, essay_class);
                    ptmt.setTimestamp(2, updatetime);
                    ptmt.setString(3, content);
                    ptmt.setString(4, EssayTitle);
                    ptmt.setInt(5, eid);
                    System.out.println(ptmt);
                    int i=ptmt.executeUpdate();
                    if(i!=0) {

                        System.out.println("文章更新成功");
                        request.getSession().setAttribute("status","1");
                        request.getSession().setAttribute("msg","文章更新成功");
                        String[] fields = {"eid","title","create_time","update_time","eclass","content"};
                        DBUtils db = new DBUtils();
                        String condition = "uid="+uid;
                        List<String[]> list1 = db.find("essay",fields,condition);
                        for (String[] arr : list1) {
                            System.out.printf("eid:%s,title:%s,create_time:%s,update_time:%s,eclass:%s,content:%s\n",arr[0],arr[1],arr[2],arr[3],arr[4],arr[5]);
                        }
                        if(list1.isEmpty()){
                            System.out.println("信息加载失败");
                        }else {
                            request.getSession().setAttribute("essayData",list1);
                       /* request.getSession().setAttribute("status","1");
                        request.getSession().setAttribute("msg","信息加载成功");*/
                            RequestDispatcher rd = request.getRequestDispatcher("management.jsp");
                            rd.forward(request,response);
                        }
                    }else {
                        System.out.println("文章更新失败");
                        request.getSession().setAttribute("status","0");
                        request.getSession().setAttribute("msg","文章更新失败");
                        RequestDispatcher rd = request.getRequestDispatcher("management.jsp");
                        rd.forward(request,response);
                    }


                rs.close();
                ptmt.close();
                conn.close();


            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
    }
}
